/*
 * dynamic_library.h
 *
 *  Created on: 2008-8-13
 *      Author: fanliangliang
 */

#ifndef DYNAMIC_LIBRARY_H_
#define DYNAMIC_LIBRARY_H_

#include "config.h"
#include <string>

#if defined(PLATFORM_WIN32)

#include <windows.h>

#define _load_library(pathname) LoadLibrary(pathname)
#define _close_library(handle) FreeLibrary(handle)
#define _lookup_symbol(handle, symbol) (void*)(GetProcAddress(handle, symbol))

typedef HINSTANCE library_handle_t;

#elif defined(PLATFORM_POSIX)

#include <dlfcn.h>

#define _load_library(pathname) dlopen(pathname, RTLD_NOW | RTLD_GLOBAL)
#define _close_library(handle) dlclose(handle)
#define _lookup_symbol(handle, symbol) dlsym(handle, symbol)

typedef void* library_handle_t;

#else
#error "DynamicLibrary unavailable on this platform"
#endif


BEGIN_NAMESPACE_TOXIC

using std::string;

class DynamicLibrary {
	CLASS_UNCOPYABLE(DynamicLibrary)
public:
	explicit DynamicLibrary(const string& pathname);
	~DynamicLibrary();

	bool isValid() const;
	const string& filename() const;
	const string& pathname() const;

	void* lookup(const string& symbol) const;

private:
	library_handle_t _handle;
	string _filename;
	string _pathname;
};

END_NAMESPACE_TOXIC

#endif /* DYNAMIC_LIBRARY_H_ */
